import java.util.HashMap;
import java.util.Map;

public class Solution219 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<nums.length;i++){
            int p=map.getOrDefault(nums[i],-1);
            if (p!=-1 && i-p<=k){
                return true;
            }else{
                map.put(nums[i],i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution219().containsNearbyDuplicate(new int[]{1,2,3,1},3));
    }
}
